﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SimulationRecorderAndPlayer.SimulationRecorderAndPlayerWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Xml;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.SimulationRecorderAndPlayer;

public class SimulationRecorderAndPlayerWindow : TxWindow, IComponentConnector
{
  private bool m_isPlaying;
  private bool _resetFlag;
  private bool _sliderTimeDrag;
  private readonly string InfoTag = "Info";
  private readonly string IsLineSimulationTag = "IsLineSimulation";
  private readonly string TimeTag = "Time";
  private readonly string AbsoluteZero = $"0{NumberFormatInfo.CurrentInfo.NumberDecimalSeparator}0";
  private SimulationRecordingIntoXML _simulationRecordingIntoXML = new SimulationRecordingIntoXML();
  private SimulationPlaybackFromXML _simulationPlaybackFromXML = new SimulationPlaybackFromXML();
  private TxSimulationPlayer _simulationPlayer = TxApplication.ActiveDocument.SimulationPlayer;
  private RecordEnum _recordState;
  private PlayBackEnum _playBackState;
  internal System.Windows.Controls.TextBox btnTextBoxBrowser;
  internal System.Windows.Controls.Button btnBrowse;
  internal System.Windows.Controls.Button btnRecording;
  internal System.Windows.Controls.Button btnStop;
  internal StackPanel TimeSP;
  internal Slider sliderTime;
  internal Popup timePopUp;
  internal Border floatingTipBorder;
  internal TextBlock sliderTextBlock;
  internal System.Windows.Controls.Button btnRest;
  internal System.Windows.Controls.Button btnStepBackward;
  internal System.Windows.Controls.Button btnPlayBackBackward;
  internal System.Windows.Controls.Button btnPausePlay;
  internal System.Windows.Controls.Button btnPlayBack;
  internal System.Windows.Controls.Button btnStepForward;
  internal System.Windows.Controls.Button btnJumpToEnd;
  internal TxNumericEditBoxControl btnTimer;
  internal StackPanel speedSP;
  internal TextBlock tbSpeed1;
  internal TextBlock tbSpeed2;
  internal Slider sliderSpeed;
  internal TextBlock tbSpeed3;
  internal TextBlock tbNote;
  internal System.Windows.Controls.Button btnClose;
  private bool _contentLoaded;

  internal RecordEnum RecordState
  {
    get => this._recordState;
    set => this._recordState = value;
  }

  internal PlayBackEnum PlayBackState
  {
    get => this._playBackState;
    set => this._playBackState = value;
  }

  public SimulationRecorderAndPlayerWindow()
  {
    this.InitializeComponent();
    ((FrameworkElement) this).DataContext = (object) this._simulationPlaybackFromXML;
    this._simulationPlayer.SimulationCleared += new TxSimulationPlayer_SimulationClearedEventHandler(this.simulationPlayer_SimulationCleared);
    this._simulationPlayer.SimulationStartedForward += new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.simulationPlayer_SimulationStartedForward);
    object obj = (object) null;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "SpeedValue", ref obj);
    double result;
    if (obj == null || !double.TryParse(obj.ToString(), out result))
      return;
    this.sliderSpeed.Value = result;
  }

  private void simulationPlayer_SimulationStartedForward(
    object sender,
    TxSimulationPlayer_SimulationStartedForwardEventArgs args)
  {
    if (this.m_isPlaying)
    {
      if (this.btnPausePlay.IsEnabled)
        this.PausePlayback();
      this.ResetPlayback();
    }
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.sliderTime.IsEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.tbNote.Visibility = Visibility.Visible;
    this.tbNote.Text = SimulationRecorderAndPlayerStringTable.RESET_NOTE;
    this.tbNote.ToolTip = (object) SimulationRecorderAndPlayerStringTable.RESET_NOTE;
  }

  private void simulationPlayer_SimulationCleared(
    object sender,
    TxSimulationPlayer_SimulationClearedEventArgs args)
  {
    if (File.Exists(this.btnTextBoxBrowser.Text) && this.btnRecording.IsEnabled)
    {
      this.btnPlayBack.IsEnabled = true;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = true;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = true;
      this.sliderTime.IsEnabled = true;
      this.sliderTime.IsMoveToPointEnabled = true;
      ((UIElement) this.btnTimer).IsEnabled = true;
    }
    this.tbNote.Text = "";
    this.tbNote.Visibility = Visibility.Collapsed;
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    this._simulationPlayer.SimulationCleared -= new TxSimulationPlayer_SimulationClearedEventHandler(this.simulationPlayer_SimulationCleared);
    this._simulationPlayer.SimulationStartedForward -= new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.simulationPlayer_SimulationStartedForward);
    if (this.m_isPlaying)
    {
      this._simulationPlaybackFromXML.PlaybackFinished -= new SimulationPlaybackFromXML.PlaybackFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackFinished);
      this._simulationPlaybackFromXML.PlaybackBackwardFinished -= new SimulationPlaybackFromXML.PlaybackBackwardFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackBackwardFinished);
      this._simulationPlaybackFromXML.Pause();
    }
    if (this.btnPausePlay.IsEnabled || this.btnRest.IsEnabled)
      this._simulationPlaybackFromXML.Reset();
    if (this._simulationRecordingIntoXML != null)
    {
      this._simulationRecordingIntoXML.UnRegisterFromSimulationEvents();
      this._simulationRecordingIntoXML.Flush();
    }
    TxWindowSettings.StoreWindowValue((TxWindow) this, "SpeedValue", (object) this.sliderSpeed.Value.ToString());
  }

  private void btnClose_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.btnTextBoxBrowser.Text = !TxTcUtilities.IsTcAnyPlatform || TxApplication.PlatformType != 2 ? $"{TxApplication.SystemRootDirectoryNormalized}{((ITxObject) TxApplication.ActiveDocument.CurrentStudy).Name}_recorded.xml" : TxApplication.ActiveDocument.FinalDestination.Replace(".psz-tc", "_recorded.xml");
  }

  private void btnStop_Click(object sender, RoutedEventArgs e)
  {
    this.btnRecording.IsEnabled = true;
    this.btnStop.IsEnabled = false;
    if (this._simulationPlayer.CurrentTime == 0.0)
    {
      this.btnPlayBack.IsEnabled = true;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = true;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = true;
      this.sliderTime.IsEnabled = true;
      this.sliderTime.IsMoveToPointEnabled = true;
      ((UIElement) this.btnTimer).IsEnabled = true;
      this.tbNote.Text = "";
      this.tbNote.Visibility = Visibility.Collapsed;
      this.EnableSpeed(true);
    }
    else
    {
      this.tbNote.Visibility = Visibility.Visible;
      this.tbNote.Text = SimulationRecorderAndPlayerStringTable.RESET_NOTE;
      this.tbNote.ToolTip = (object) SimulationRecorderAndPlayerStringTable.RESET_NOTE;
    }
    this.btnRest.IsEnabled = false;
    this.btnPausePlay.IsEnabled = false;
    this.btnBrowse.IsEnabled = true;
    this.btnTextBoxBrowser.IsEnabled = true;
    this.EnableSpeed(true);
    this._simulationRecordingIntoXML.UnRegisterFromSimulationEvents();
    this._simulationRecordingIntoXML.Flush();
    this._simulationPlaybackFromXML.GetTimeTable(this.btnTextBoxBrowser.Text);
    this.sliderTime.Maximum = (double) (this._simulationPlaybackFromXML.IndexToTime.Count - 1);
    TxApplication.RefreshDisplay();
  }

  private void btnRecording_Click(object sender, RoutedEventArgs e)
  {
    if (!this._simulationRecordingIntoXML.Init(this.btnTextBoxBrowser.Text))
      return;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = true;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.sliderTime.IsEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.btnPausePlay.IsEnabled = false;
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.EnableSpeed(false);
    this.tbNote.Text = "";
    this.tbNote.Visibility = Visibility.Collapsed;
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.FilePath = "";
    this._simulationRecordingIntoXML.RegisterToSimulationEvents();
  }

  private void btnPlayback_Click(object sender, RoutedEventArgs e)
  {
    this.m_isPlaying = true;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.sliderTime.IsEnabled = true;
    this.sliderTime.IsMoveToPointEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.btnPausePlay.IsEnabled = true;
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.EnableSpeed(true);
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.PlaybackFinished += new SimulationPlaybackFromXML.PlaybackFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackFinished);
    this._simulationPlaybackFromXML.PlayForward(this.btnTextBoxBrowser.Text, PlayModeEnum.PlayForward);
  }

  private void btnPlaybackBackward_Click(object sender, RoutedEventArgs e)
  {
    this.m_isPlaying = true;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.sliderTime.IsEnabled = true;
    this.sliderTime.IsMoveToPointEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.btnPausePlay.IsEnabled = true;
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.EnableSpeed(true);
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.PlaybackBackwardFinished += new SimulationPlaybackFromXML.PlaybackBackwardFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackBackwardFinished);
    this._simulationPlaybackFromXML.PlayBackward(PlayModeEnum.PlayBackward);
  }

  private void btnStepBackward_Click(object sender, RoutedEventArgs e)
  {
    this.m_isPlaying = true;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.sliderTime.IsEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.btnPausePlay.IsEnabled = true;
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.EnableSpeed(true);
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.StepBackward += new SimulationPlaybackFromXML.StepBackwardEventHandler(this._simulationPlaybackFromXML_StepBackward);
    this._simulationPlaybackFromXML.PlaybackBackwardFinished += new SimulationPlaybackFromXML.PlaybackBackwardFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackBackwardFinished);
    this._simulationPlaybackFromXML.PlayBackward(PlayModeEnum.StepBackward);
    this.m_isPlaying = false;
  }

  private void btnStepForward_Click(object sender, RoutedEventArgs e)
  {
    this.m_isPlaying = true;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.sliderTime.IsEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.btnPausePlay.IsEnabled = true;
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.EnableSpeed(true);
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.StepForward += new SimulationPlaybackFromXML.StepForwardEventHandler(this._simulationPlaybackFromXML_StepForward);
    this._simulationPlaybackFromXML.PlaybackFinished += new SimulationPlaybackFromXML.PlaybackFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackFinished);
    this._simulationPlaybackFromXML.PlayForward(this.btnTextBoxBrowser.Text, PlayModeEnum.StepForward);
    this.m_isPlaying = false;
  }

  private void btnJumpToEnd_Click(object sender, RoutedEventArgs e)
  {
    this._resetFlag = true;
    this.m_isPlaying = true;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.sliderTime.IsEnabled = true;
    this.sliderTime.IsMoveToPointEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
    this.btnPausePlay.IsEnabled = true;
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.EnableSpeed(true);
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.PlaybackFinished += new SimulationPlaybackFromXML.PlaybackFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackFinished);
    this._simulationPlaybackFromXML.PlayForward(this.btnTextBoxBrowser.Text, PlayModeEnum.JumpToEnd);
    this._resetFlag = false;
  }

  private void _simulationPlaybackFromXML_PlaybackFinished(object sender, EventArgs args)
  {
    this.m_isPlaying = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = true;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = true;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = true;
    this.sliderTime.IsEnabled = true;
    this.sliderTime.IsMoveToPointEnabled = true;
    ((UIElement) this.btnTimer).IsEnabled = true;
    ((UIElement) this.btnTimer).IsEnabled = true;
    this.btnPausePlay.IsEnabled = false;
    this._simulationPlaybackFromXML.PlaybackFinished -= new SimulationPlaybackFromXML.PlaybackFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackFinished);
  }

  private void _simulationPlaybackFromXML_PlaybackBackwardFinished(object sender, EventArgs args)
  {
    this._resetFlag = true;
    this.m_isPlaying = false;
    this.btnRest.IsEnabled = true;
    this.btnPlayBack.IsEnabled = true;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = true;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = true;
    this.sliderTime.IsEnabled = true;
    this.sliderTime.IsMoveToPointEnabled = true;
    ((UIElement) this.btnTimer).IsEnabled = true;
    this.btnPausePlay.IsEnabled = false;
    this._simulationPlaybackFromXML.PlaybackBackwardFinished -= new SimulationPlaybackFromXML.PlaybackBackwardFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackBackwardFinished);
    this._resetFlag = false;
  }

  private void _simulationPlaybackFromXML_StepBackward(object sender, EventArgs args)
  {
    this.btnRest.IsEnabled = true;
    bool flag = this.btnTimer.Text != this.AbsoluteZero;
    this.btnPlayBackBackward.IsEnabled = flag;
    this.btnStepBackward.IsEnabled = flag;
    this.btnPlayBack.IsEnabled = true;
    this.btnJumpToEnd.IsEnabled = true;
    this.btnStepForward.IsEnabled = true;
    this.btnPausePlay.IsEnabled = false;
    this.sliderTime.IsEnabled = true;
    ((UIElement) this.btnTimer).IsEnabled = true;
    this._simulationPlaybackFromXML.PlaybackBackwardFinished -= new SimulationPlaybackFromXML.PlaybackBackwardFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackBackwardFinished);
    this._simulationPlaybackFromXML.StepBackward -= new SimulationPlaybackFromXML.StepBackwardEventHandler(this._simulationPlaybackFromXML_StepBackward);
  }

  private void _simulationPlaybackFromXML_StepForward(object sender, EventArgs args)
  {
    this.btnRest.IsEnabled = true;
    bool flag = this.btnTimer.Text != this.AbsoluteZero;
    this.btnPlayBackBackward.IsEnabled = flag;
    this.btnStepBackward.IsEnabled = flag;
    this.btnPlayBack.IsEnabled = true;
    this.btnJumpToEnd.IsEnabled = true;
    this.btnStepForward.IsEnabled = true;
    this.sliderTime.IsEnabled = true;
    ((UIElement) this.btnTimer).IsEnabled = true;
    this.btnPausePlay.IsEnabled = false;
    this._simulationPlaybackFromXML.PlaybackFinished -= new SimulationPlaybackFromXML.PlaybackFinishedEventHandler(this._simulationPlaybackFromXML_PlaybackFinished);
    this._simulationPlaybackFromXML.StepForward -= new SimulationPlaybackFromXML.StepForwardEventHandler(this._simulationPlaybackFromXML_StepForward);
  }

  private void btnPause_Click(object sender, RoutedEventArgs e) => this.PausePlayback();

  private void btnRest_Click(object sender, RoutedEventArgs e) => this.ResetPlayback();

  private void btnBrowse_Click(object sender, RoutedEventArgs e)
  {
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.DefaultExt = ".xml";
    openFileDialog.Filter = "xml file (.xml)|*.xml";
    openFileDialog.CheckFileExists = false;
    string str;
    try
    {
      str = Path.GetDirectoryName(this.btnTextBoxBrowser.Text);
    }
    catch (ArgumentException ex)
    {
      str = TxApplication.SystemRootDirectoryNormalized;
    }
    openFileDialog.InitialDirectory = str;
    openFileDialog.Title = SimulationRecorderAndPlayerStringTable.BROWSE;
    openFileDialog.SupportMultiDottedExtensions = true;
    if (openFileDialog.ShowDialog() != DialogResult.OK)
      return;
    this.btnTextBoxBrowser.Text = openFileDialog.FileName;
    if (this.btnTextBoxBrowser.Text.Contains(".xml"))
      return;
    this.btnTextBoxBrowser.Text += ".xml";
  }

  private void EnableSpeed(bool shouldEnable)
  {
    this.tbSpeed1.IsEnabled = shouldEnable;
    this.tbSpeed2.IsEnabled = shouldEnable;
    this.tbSpeed3.IsEnabled = shouldEnable;
    this.sliderSpeed.IsEnabled = shouldEnable;
  }

  private void CheckTheCorrectSimulationMode()
  {
    if (!File.Exists(this.btnTextBoxBrowser.Text))
      return;
    XmlDocument xmlDocument = new XmlDocument();
    try
    {
      xmlDocument.Load(this.btnTextBoxBrowser.Text);
    }
    catch (Exception ex)
    {
      int num = (int) TxMessageBox.Show($"{SimulationRecorderAndPlayerStringTable.FAILED_TO_OPEN_FILE}\n{ex.Message}", SimulationRecorderAndPlayerStringTable.MESSAGEBOX_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      return;
    }
    XmlNodeList elementsByTagName1 = xmlDocument.GetElementsByTagName(this.InfoTag);
    if (elementsByTagName1 == null || elementsByTagName1.Count <= 0)
      return;
    string str = elementsByTagName1[0].Attributes.GetNamedItem(this.IsLineSimulationTag)?.Value ?? "";
    if ((TxEngineeringDataInternal.GlobalGetPartsMode() == 2).ToString().ToLower() != str)
    {
      if (TxApplication.ActiveDocument.SimulationPlayer.IsLineSimulation)
      {
        this.tbNote.Visibility = Visibility.Visible;
        this.tbNote.Text = SimulationRecorderAndPlayerStringTable.LINE_SIMULATION_IS_NEDDED;
        this.tbNote.ToolTip = (object) SimulationRecorderAndPlayerStringTable.LINE_SIMULATION_IS_NEDDED;
      }
      else
      {
        this.tbNote.Visibility = Visibility.Visible;
        this.tbNote.Text = SimulationRecorderAndPlayerStringTable.STNANDARD_MODE_IS_NEDDED;
        this.tbNote.ToolTip = (object) SimulationRecorderAndPlayerStringTable.STNANDARD_MODE_IS_NEDDED;
      }
      this.btnPlayBack.IsEnabled = false;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = false;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = false;
      this.btnRest.IsEnabled = false;
      this.btnPausePlay.IsEnabled = false;
      this.sliderTime.IsEnabled = false;
      ((UIElement) this.btnTimer).IsEnabled = false;
      this.EnableSpeed(false);
    }
    else
    {
      XmlNodeList elementsByTagName2 = xmlDocument.GetElementsByTagName(this.TimeTag);
      this._simulationPlaybackFromXML.GetTimeTable(xmlTimeNodeList: elementsByTagName2);
      this.sliderTime.Maximum = (double) (elementsByTagName2.Count - 1);
    }
  }

  private void btnTextBoxBrowser_TextChanged(object sender, TextChangedEventArgs e)
  {
    if (!File.Exists(this.btnTextBoxBrowser.Text))
    {
      this.btnPlayBack.IsEnabled = false;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = false;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = false;
      this.btnRest.IsEnabled = false;
      this.btnPausePlay.IsEnabled = false;
      this.sliderTime.IsEnabled = false;
      ((UIElement) this.btnTimer).IsEnabled = false;
      this.tbNote.Text = "";
      this.tbNote.Visibility = Visibility.Collapsed;
      this.EnableSpeed(false);
    }
    else if (this._simulationPlayer.CurrentTime == 0.0)
    {
      this.btnPlayBack.IsEnabled = true;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = true;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = true;
      this.btnRest.IsEnabled = false;
      this.btnPausePlay.IsEnabled = false;
      this.sliderTime.IsEnabled = true;
      this.sliderTime.IsMoveToPointEnabled = true;
      ((UIElement) this.btnTimer).IsEnabled = true;
      this.tbNote.Text = "";
      this.tbNote.Visibility = Visibility.Collapsed;
      this.EnableSpeed(true);
      this._simulationPlaybackFromXML.FilePath = "";
    }
    else
    {
      this.tbNote.Visibility = Visibility.Visible;
      this.tbNote.Text = SimulationRecorderAndPlayerStringTable.RESET_NOTE;
      this.tbNote.ToolTip = (object) SimulationRecorderAndPlayerStringTable.RESET_NOTE;
      this.btnPlayBack.IsEnabled = false;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = false;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = false;
      this.btnRest.IsEnabled = false;
      this.btnPausePlay.IsEnabled = false;
      this.sliderTime.IsEnabled = false;
      ((UIElement) this.btnTimer).IsEnabled = false;
      this.EnableSpeed(false);
    }
    if (this.btnTextBoxBrowser.Text != "")
    {
      bool flag = false;
      try
      {
        flag = Directory.Exists(Path.GetDirectoryName(this.btnTextBoxBrowser.Text));
      }
      catch
      {
      }
      if (!flag)
      {
        this.tbNote.Text = "";
        this.tbNote.Visibility = Visibility.Collapsed;
        this.btnRecording.IsEnabled = false;
        this.EnableSpeed(false);
      }
      else
      {
        try
        {
          if (new FileInfo(this.btnTextBoxBrowser.Text).Extension == ".xml")
            this.btnRecording.IsEnabled = true;
          else
            this.btnRecording.IsEnabled = false;
        }
        catch
        {
          this.btnRecording.IsEnabled = false;
        }
      }
      this.CheckTheCorrectSimulationMode();
    }
    else
    {
      this.tbNote.Text = "";
      this.tbNote.Visibility = Visibility.Collapsed;
      this.btnRecording.IsEnabled = false;
      this.EnableSpeed(false);
    }
  }

  private void sliderSpeed_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  {
    this._simulationPlaybackFromXML.Speed = this.sliderSpeed.Value;
  }

  private void PausePlayback()
  {
    this._resetFlag = true;
    this.m_isPlaying = false;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    if (this._simulationPlayer.CurrentTime == 0.0)
    {
      this.btnPlayBack.IsEnabled = true;
      this.btnPlayBackBackward.IsEnabled = true;
      this.btnJumpToEnd.IsEnabled = true;
      this.btnStepBackward.IsEnabled = true;
      this.btnStepForward.IsEnabled = true;
      this.tbNote.Text = "";
      this.tbNote.Visibility = Visibility.Collapsed;
    }
    this.btnRest.IsEnabled = true;
    this.btnPausePlay.IsEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = true;
    this.sliderTime.IsMoveToPointEnabled = true;
    TxApplication.RefreshDisplay();
    this._simulationPlaybackFromXML.Pause();
    this._resetFlag = false;
  }

  private void ResetPlayback()
  {
    this._resetFlag = true;
    this.m_isPlaying = false;
    this._simulationPlaybackFromXML.Reset();
    this._simulationPlaybackFromXML.CurrentTimer = this.AbsoluteZero;
    this.btnRecording.IsEnabled = true;
    this.btnStop.IsEnabled = false;
    if (this._simulationPlayer.CurrentTime == 0.0)
    {
      this.btnPlayBack.IsEnabled = true;
      this.btnPlayBackBackward.IsEnabled = false;
      this.btnJumpToEnd.IsEnabled = true;
      this.btnStepBackward.IsEnabled = false;
      this.btnStepForward.IsEnabled = true;
      this.tbNote.Text = "";
      this.tbNote.Visibility = Visibility.Collapsed;
      this.EnableSpeed(true);
    }
    this.btnRest.IsEnabled = false;
    this.btnPausePlay.IsEnabled = false;
    this.btnBrowse.IsEnabled = true;
    this.btnTextBoxBrowser.IsEnabled = true;
    this._resetFlag = false;
  }

  private void jumpStarted()
  {
    this.btnBrowse.IsEnabled = false;
    this.btnTextBoxBrowser.IsEnabled = false;
    this.btnRecording.IsEnabled = false;
    this.btnStop.IsEnabled = false;
    this.btnPlayBack.IsEnabled = false;
    this.btnPlayBackBackward.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = false;
    this.btnStepBackward.IsEnabled = false;
    this.btnStepForward.IsEnabled = false;
    this.btnRest.IsEnabled = false;
    this.btnPausePlay.IsEnabled = false;
    ((UIElement) this.btnTimer).IsEnabled = false;
  }

  private void jumpCompleted()
  {
    bool flag1 = this.sliderTime.Value != 0.0;
    bool flag2 = this.sliderTime.Value != (double) (this._simulationPlaybackFromXML.IndexToTime.Count - 1);
    this.btnPlayBackBackward.IsEnabled = flag1;
    this.btnStepBackward.IsEnabled = flag1;
    this.btnPlayBackBackward.IsEnabled = flag1;
    ((UIElement) this.btnTimer).IsEnabled = true;
    this.btnRest.IsEnabled = true;
    this.btnPlayBack.IsEnabled = flag2;
    this.btnStepForward.IsEnabled = flag2;
    this.btnPausePlay.IsEnabled = false;
    this.btnJumpToEnd.IsEnabled = flag2;
  }

  private void btnTimer_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
    if (e.Key != Key.Return)
      return;
    this.GetJumpFrom_btnTimer();
  }

  private void btnTimer_LostFocus(object sender, RoutedEventArgs e) => this.GetJumpFrom_btnTimer();

  private void GetJumpFrom_btnTimer()
  {
    int num = Math.Abs(new List<double>((IEnumerable<double>) this._simulationPlaybackFromXML.IndexToTime.Values).BinarySearch(this.btnTimer.Value));
    this.btnTimer.Value = double.Parse(this._simulationPlaybackFromXML.CurrentTimer);
    this._simulationPlaybackFromXML.JumpTimeIndex = num;
  }

  private void sliderTime_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  {
    if (this.m_isPlaying || this._resetFlag || this._sliderTimeDrag)
      return;
    this.JumpToTime();
  }

  private void sliderTime_DragStarted(object sender, DragStartedEventArgs e)
  {
    this._sliderTimeDrag = true;
    this.jumpStarted();
    if (!this.m_isPlaying)
      return;
    this._simulationPlaybackFromXML.Pause();
    this.m_isPlaying = false;
  }

  private void sliderTime_DragCompleted(object sender, DragCompletedEventArgs e)
  {
    this.JumpToTime();
    this._sliderTimeDrag = false;
  }

  private void JumpToTime()
  {
    if (!this._sliderTimeDrag)
      this.jumpStarted();
    int jumpTimeIndex = this._simulationPlaybackFromXML.JumpTimeIndex;
    int currentTimeIndex = this._simulationPlaybackFromXML.CurrentTimeIndex;
    if (jumpTimeIndex > currentTimeIndex)
      this._simulationPlaybackFromXML.PlayForward(this.btnTextBoxBrowser.Text, PlayModeEnum.JumpToTime);
    else if (jumpTimeIndex < currentTimeIndex)
      this._simulationPlaybackFromXML.PlayBackward(PlayModeEnum.JumpToTime);
    this.jumpCompleted();
  }

  private void sliderTime_MouseWheel(object sender, MouseWheelEventArgs e)
  {
    if (e.Delta > 0)
    {
      ++this.sliderTime.Value;
    }
    else
    {
      if (e.Delta >= 0)
        return;
      --this.sliderTime.Value;
    }
  }

  private void sliderTime_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  {
    if (!this.timePopUp.IsOpen)
      this.timePopUp.IsOpen = true;
    Point position = e.GetPosition((IInputElement) this.sliderTime);
    if (this._sliderTimeDrag)
      this.UpdatePopUpTime(this._simulationPlaybackFromXML.IndexToTime[(int) this.sliderTime.Value]);
    else
      this.UpdatePopUpTime(this.GetTimeFromBarLocation(position.X));
    this.timePopUp.HorizontalOffset = position.X - this.floatingTipBorder.ActualWidth / 2.0;
    this.timePopUp.VerticalOffset = -32.0;
  }

  private void sliderTime_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
  {
    this.timePopUp.IsOpen = false;
  }

  private double GetTimeFromBarLocation(double loc)
  {
    loc = loc < 0.0 ? 0.0 : loc;
    double num = loc * (double) this._simulationPlaybackFromXML.IndexToTime.Count / this.sliderTime.Width;
    return this._simulationPlaybackFromXML.IndexToTime[(int) Math.Round(num > (double) (this._simulationPlaybackFromXML.IndexToTime.Count - 1) ? (double) (this._simulationPlaybackFromXML.IndexToTime.Count - 1) : num)];
  }

  private void UpdatePopUpTime(double time)
  {
    int afterDecimalPoint = TxApplication.Options.Units.TimeDigitsAfterDecimalPoint;
    time = Math.Round(time, afterDecimalPoint);
    this.sliderTextBlock.Text = time.ToString($"F{afterDecimalPoint}");
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/simulationrecorderandplayer/simulationrecorderandplayerwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        break;
      case 2:
        this.btnTextBoxBrowser = (System.Windows.Controls.TextBox) target;
        this.btnTextBoxBrowser.TextChanged += new TextChangedEventHandler(this.btnTextBoxBrowser_TextChanged);
        break;
      case 3:
        this.btnBrowse = (System.Windows.Controls.Button) target;
        this.btnBrowse.Click += new RoutedEventHandler(this.btnBrowse_Click);
        break;
      case 4:
        this.btnRecording = (System.Windows.Controls.Button) target;
        this.btnRecording.Click += new RoutedEventHandler(this.btnRecording_Click);
        break;
      case 5:
        this.btnStop = (System.Windows.Controls.Button) target;
        this.btnStop.Click += new RoutedEventHandler(this.btnStop_Click);
        break;
      case 6:
        this.TimeSP = (StackPanel) target;
        break;
      case 7:
        this.sliderTime = (Slider) target;
        this.sliderTime.MouseLeave += new System.Windows.Input.MouseEventHandler(this.sliderTime_MouseLeave);
        this.sliderTime.MouseMove += new System.Windows.Input.MouseEventHandler(this.sliderTime_MouseMove);
        this.sliderTime.AddHandler(Thumb.DragCompletedEvent, (Delegate) new DragCompletedEventHandler(this.sliderTime_DragCompleted));
        this.sliderTime.AddHandler(Thumb.DragStartedEvent, (Delegate) new DragStartedEventHandler(this.sliderTime_DragStarted));
        this.sliderTime.ValueChanged += new RoutedPropertyChangedEventHandler<double>(this.sliderTime_ValueChanged);
        this.sliderTime.MouseWheel += new MouseWheelEventHandler(this.sliderTime_MouseWheel);
        break;
      case 8:
        this.timePopUp = (Popup) target;
        break;
      case 9:
        this.floatingTipBorder = (Border) target;
        break;
      case 10:
        this.sliderTextBlock = (TextBlock) target;
        break;
      case 11:
        this.btnRest = (System.Windows.Controls.Button) target;
        this.btnRest.Click += new RoutedEventHandler(this.btnRest_Click);
        break;
      case 12:
        this.btnStepBackward = (System.Windows.Controls.Button) target;
        this.btnStepBackward.Click += new RoutedEventHandler(this.btnStepBackward_Click);
        break;
      case 13:
        this.btnPlayBackBackward = (System.Windows.Controls.Button) target;
        this.btnPlayBackBackward.Click += new RoutedEventHandler(this.btnPlaybackBackward_Click);
        break;
      case 14:
        this.btnPausePlay = (System.Windows.Controls.Button) target;
        this.btnPausePlay.Click += new RoutedEventHandler(this.btnPause_Click);
        break;
      case 15:
        this.btnPlayBack = (System.Windows.Controls.Button) target;
        this.btnPlayBack.Click += new RoutedEventHandler(this.btnPlayback_Click);
        break;
      case 16 /*0x10*/:
        this.btnStepForward = (System.Windows.Controls.Button) target;
        this.btnStepForward.Click += new RoutedEventHandler(this.btnStepForward_Click);
        break;
      case 17:
        this.btnJumpToEnd = (System.Windows.Controls.Button) target;
        this.btnJumpToEnd.Click += new RoutedEventHandler(this.btnJumpToEnd_Click);
        break;
      case 18:
        this.btnTimer = (TxNumericEditBoxControl) target;
        ((UIElement) this.btnTimer).KeyDown += new System.Windows.Input.KeyEventHandler(this.btnTimer_KeyDown);
        ((UIElement) this.btnTimer).LostFocus += new RoutedEventHandler(this.btnTimer_LostFocus);
        break;
      case 19:
        this.speedSP = (StackPanel) target;
        break;
      case 20:
        this.tbSpeed1 = (TextBlock) target;
        break;
      case 21:
        this.tbSpeed2 = (TextBlock) target;
        break;
      case 22:
        this.sliderSpeed = (Slider) target;
        this.sliderSpeed.ValueChanged += new RoutedPropertyChangedEventHandler<double>(this.sliderSpeed_ValueChanged);
        break;
      case 23:
        this.tbSpeed3 = (TextBlock) target;
        break;
      case 24:
        this.tbNote = (TextBlock) target;
        break;
      case 25:
        this.btnClose = (System.Windows.Controls.Button) target;
        this.btnClose.Click += new RoutedEventHandler(this.btnClose_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
